<?php

namespace App\Http\Controllers\home\lei;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Good;
use Illuminate\Support\Facades\Input;
use DB;

class GoodController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = DB::table('goods')->paginate(7);
        return view('home.lei.good.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home.lei.good.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token');
        $good = new Good;
        $good -> g_name = $data['g_name'];
        $good -> g_price = $data['g_price'];
        $good -> g_description = $data['g_description'];
        $good -> g_logo = basename($data['g_logo']);
        $res = $good ->save();
        if($res){
            return redirect('/home/good');
        }else{
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Good::find($id);
        return view('home.lei.good.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token');
        $good = Good::find($id);
        $good -> g_name = $data['g_name'];
        $good -> g_price = $data['g_price'];
        $good -> g_description = $data['g_description'];
        $good -> g_logo = basename($data['g_logo']);
        $res = $good -> save();
        if($res){
            return redirect('/home/good');
        }else{
            return back();
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = [];
       $re =  Good::find($id)->delete();
       //删除成功
       if($re){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;
    }

    public function goodUpload(Request $request)
    {
//        获取文件对象
        $file = Input::file('file_license');
//        判断文件是否有效
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;
            $path = $file->move(public_path().'/lei/home/images',$newName);
            $filepath = 'lei/home/images/'.$newName;
            return  $filepath;
        }
    }
}
